package com.dwms.interceptor;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;
import java.net.URL;

@Slf4j
@Component
public class OpenInterceptor implements HandlerInterceptor {

    /**
     * 属性
     */
    private final static String REQUEST_START_TIME_ATTR = "startTime";

    private final static String[] IGNORE_URI = new String[]{"/api/v1/gateway", "/hs/notify/gateway"};

    /**
     * 请求拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //解决跨域
        response.setHeader("Access-Control-Allow-Origin", "*");
        String requestUri = request.getRequestURI().replace("//", "/");
        StringBuffer requestUrl = request.getRequestURL();
        long startTime = System.currentTimeMillis();
        URL url = new URL(requestUrl.toString());
        request.setAttribute(REQUEST_START_TIME_ATTR, startTime);
//        log.info("通知端，请求requestUrl信息:" + requestUrl);
//        log.info("通知端，请求tempContextUrl信息:" + url.getHost());
//        log.info("通知端，requestUri:" + requestUri);
        String referer = request.getHeader("referer");
        //直接放过的请求
        if (requestUri.contains("swagger") || (StrUtil.isNotEmpty(referer) && referer.contains("swagger"))) {
            return true;
        }
        return ArrayUtils.contains(IGNORE_URI, requestUri);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        long startTime = (Long) request.getAttribute(REQUEST_START_TIME_ATTR);
        long endTime = System.currentTimeMillis();
        long consumeTime = endTime - startTime;
       // log.info("postHandle-response.getStatus()：" + response.getStatus());
        //log.info("postHandle访问目标:" + method.getDeclaringClass().getName() + "." + method.getName() + "，消耗时间：" + consumeTime + " ms");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
//        log.info("afterCompletion-response.getStatus()：" + response.getStatus());
//        log.info("afterCompletion-getShortLogMessage：" + handlerMethod.getShortLogMessage());
//        log.info("afterCompletion-访问目标：" + method.getDeclaringClass().getName() + "." + method.getName() + " 结束");
    }
}
